﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;
using MSIACCSL2.Web.WarehouseServices;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class SPK : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument spk;
        private ReportDocument spkDetail;
        private string Tipe;

        public SPK()
        {
            DA = new DataAccess<object>();
            spk = new ReportDocument();
            spkDetail = new ReportDocument();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
            if (IsPostBack)
                LoadReport(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
        }
      
    
        protected void Button1_Click(object sender, EventArgs e)
        {
          LoadReport(Request.QueryString["no"].ToString(), ViewState["tipe"].ToString());
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
        }


        public void LoadReport(string NoSO,string tipe)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(
                " Select a.NoSPK,a.SPKDate,a.Type,a.DeliveryAddress,a.Rate,a.DueDate, a.Ppn as tax,a.Keterangan, ");
            sql.Append(" a.Discount,a.CurrencyCode,a.DeliveryCode,a.CustomerName,a.Address, a.Telp, ");
            sql.Append(" (Case When a.DiscountType is Null then '' Else a.DiscountType End) as DiscountType, ");
            sql.Append(" b.Qty,b.Price,b.Discount as DiscountD,  ");
            sql.Append(" (Case When b.DiscountType is Null then '' Else b.DiscountType End) as DiscountTypeD,  ");
            sql.Append(" it.SerialNumber, b.Panjang,b.Lebar,b.Tinggi,it.SatuanUnit, ");
            sql.Append(" Case when sg.SubgroupName IS NULL THEN sg2.SubGroupName ELSE sg.SubGroupName END AS SubgroupName , ");
            sql.Append(" Case when i.ItemName IS NULL THEN i2.ItemName ELSE i.ItemName END AS ItemName , ");
            sql.Append(" sm.NamaDepan as Salesman  ");
            sql.Append(" from SPK a  ");
            sql.Append(" inner JOIN SpkDetail  b on a.NoSPK = b.NoSPK   ");
            sql.Append(" left Join Inventory it on it.SerialNumber = b.SerialNumber    ");
            sql.Append(" left Join Item i on i.ItemId = it.ItemId    ");
            sql.Append(" LEFT JOIN SubGroup sg ON sg.SubGroupID = i.SubGroupID  ");
            sql.Append(" LEFT JOIN Item i2 ON i2.ItemId = b.ItemId ");
            sql.Append(" LEFT JOIN SubGroup sg2 ON sg2.SubGroupID = b.SubGroupId ");
            sql.Append(" LEFT JOIN Salesman sm ON sm.KodeSalesman = a.KodeSalesman  ");
            sql.AppendFormat(" Where a.NoSPK='{0}'  Order By sg.SubgroupId,i.ItemId   ", NoSO);
            var Source = DA.GetDataListDataSet(sql.ToString());

            if (RadioButtonList2.SelectedValue == "Summary")
            {
                string reportPath = Server.MapPath("~/Report/SPK.rpt");
                spk.Load(reportPath);
                spk.SetDataSource(Source.Tables[0]);
                var param = new ParameterDiscreteValue();
                param.Value = tipe;
                var paramValue = new ParameterValues();
                paramValue.Add(param);
                spk.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
                CrystalReportViewer1.ReportSource = spk;
            }
            else
            {
                string reportPath = Server.MapPath("~/Report/SPKDetail.rpt");
                spkDetail.Load(reportPath);
                spkDetail.SetDataSource(Source.Tables[0]);
                var param = new ParameterDiscreteValue();
                param.Value = tipe;
                var paramValue = new ParameterValues();
                paramValue.Add(param);
                spkDetail.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
                CrystalReportViewer1.ReportSource = spkDetail;
            }

        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            spk.Clone();
            spk.Dispose();

            spkDetail.Clone();
            spkDetail.Dispose();
        }
    }
}